﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class Common
    {
        public void ChageOrderID(int flag, int NowID, string ModelID, string tblName)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@flag", SqlDbType.Int), new SqlParameter("@NowID", SqlDbType.Int), new SqlParameter("@ModelID", SqlDbType.NText), new SqlParameter("@tblName", SqlDbType.NText) };
            parameters[0].Value = flag;
            parameters[1].Value = NowID;
            parameters[2].Value = ModelID;
            parameters[3].Value = tblName;
            DbHelperSQL.RunProcedure("sp_ChangeOrderID", parameters, out num);
        }

        public string DatabaseBackup(string BakPath)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@BakPath", SqlDbType.NText) };
            parameters[0].Value = BakPath;
            return DbHelperSQL.RunProcedure("sp_DatabaseBackup", parameters, "ds").Tables[0].Rows[0][0].ToString();
        }

        public void DatabaseLogDel()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            DbHelperSQL.RunProcedure("sp_DatabaseLogDel", parameters, "ds");
        }

        public string DatabaseName()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            return DbHelperSQL.RunProcedure("sp_DatabaseName", parameters, "ds").Tables[0].Rows[0][0].ToString();
        }

        public DataSet DatabaseTable()
        {
            SqlParameter[] parameters = new SqlParameter[0];
            return DbHelperSQL.RunProcedure("sp_DatabaseTable", parameters, "ds");
        }

        public DataSet DatabaseTable(string tblName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.VarChar) };
            parameters[0].Value = tblName;
            return DbHelperSQL.RunProcedure("TableColumnDescription", parameters, "ds");
        }

        public static void ExecuteSQL(string sql)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StrSQL", SqlDbType.NText) };
            parameters[0].Value = sql;
            DbHelperSQL.RunProcedure("sp_ExecSQL", parameters);
        }

        public int GetCount(string tblName, string strWhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            return int.Parse(DbHelperSQL.RunProcedure("sp_GetCount", parameters, "ds").Tables[0].Rows[0][0].ToString());
        }

        public DataSet GetDataTableBySql(string sql)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StrSQL", SqlDbType.NText) };
            parameters[0].Value = sql;
            return DbHelperSQL.RunProcedure("sp_ExecSQL", parameters, "ds");
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = "*";
            parameters[3].Value = strOrder;
            parameters[4].Value = PageSize;
            parameters[5].Value = PageIndex;
            return DbHelperSQL.RunProcedure("sp_GetList", parameters, "ds");
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = fldName;
            parameters[3].Value = strOrder;
            parameters[4].Value = PageSize;
            parameters[5].Value = PageIndex;
            return DbHelperSQL.RunProcedure("sp_GetList", parameters, "ds");
        }

        public DataSet GetList(string tblName, string strWhere, string strOrder, string fldName, int rowStart, int rowEnd)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText), new SqlParameter("@rowStart", SqlDbType.Int), new SqlParameter("@rowEnd", SqlDbType.Int) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = fldName;
            parameters[3].Value = strOrder;
            parameters[4].Value = rowStart;
            parameters[5].Value = rowEnd;
            return DbHelperSQL.RunProcedure("sp_GetListByRow", parameters, "ds");
        }

        public DataSet GetList(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string tblLink, string fldLink, string conLink)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int), new SqlParameter("@tblLink", SqlDbType.NText), new SqlParameter("@fldLink", SqlDbType.NText), new SqlParameter("@conLink", SqlDbType.NText) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = "*";
            parameters[3].Value = strOrder;
            parameters[4].Value = PageSize;
            parameters[5].Value = PageIndex;
            parameters[6].Value = tblLink;
            parameters[7].Value = fldLink;
            parameters[8].Value = conLink;
            return DbHelperSQL.RunProcedure("sp_GetListForLink", parameters, "ds");
        }

        public DataSet GetListForMoreTable(string tblName, int PageSize, int PageIndex, string strWhere, string strOrder, string fldName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = fldName;
            parameters[3].Value = strOrder;
            parameters[4].Value = PageSize;
            parameters[5].Value = PageIndex;
            return DbHelperSQL.RunProcedure("sp_GetListForMoreTable", parameters, "ds");
        }

        public int GetMaxOrderID(string tblName, string fldName)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText) };
            parameters[0].Value = tblName;
            parameters[1].Value = fldName;
            return DbHelperSQL.RunProcedure("sp_GetMaxOrderID", parameters, out num);
        }

        public DataSet GetModel(string tblName, string strWhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            return DbHelperSQL.RunProcedure("sp_GetModel", parameters, "ds");
        }

        public DataSet GetSelect(string tblName, string strWhere, string strOrder, string fldName)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@tblName", SqlDbType.NText), new SqlParameter("@strWhere", SqlDbType.NText), new SqlParameter("@fldName", SqlDbType.NText), new SqlParameter("@strOrder", SqlDbType.NText) };
            parameters[0].Value = tblName;
            parameters[1].Value = strWhere;
            parameters[2].Value = fldName;
            parameters[3].Value = strOrder;
            return DbHelperSQL.RunProcedure("sp_GetSelect", parameters, "ds");
        }
    }
}

